package com.youge.springcloud.consumer.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RetryRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author: hcf
 * @qq: 46914685
 * @email: 46914685@qq.com
 * @date: 2020-10-20 23:47
 */
@Configuration //等价于一个spring applicationcontext.xml配置文件
public class BeanConfig {

    /**
     * @Bean等价于配置文件中
     * <bean id="restTemplate" class="xxxxxxx.xx.xx.RestTemplate"></bean>
     *
     * @LoadBalanced 启用ribbon负载均衡
     *
     * @return
     */
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /**
     * 覆盖掉原来ribbon默认的轮循
     * @return
     */
    @Bean
    public IRule iRule(){
//        return new RandomRule();//采用随机的负载均衡策略
        return new RetryRule();//采用重试的负载均衡策略
    }
}
